package yzserver.sx4a.district;

import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IDistrictDao;
import yzserver.sx4a.domain.DistrictDomain;
import yzserver.sx4a.form.District;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aDistrictImpl implements Sx4aDistrict {

    private IDistrictDao dictrictDao;

    public void setDictrictDao(IDistrictDao dictrictDao) {
        this.dictrictDao = dictrictDao;
    }

    @Override
    public District selDistrict(String districtid) {
        DistrictDomain domain = dictrictDao.selDistrict(districtid);
        if(domain!=null){
            District district = new District(domain);
            return district;
        }
        else{
            return null;
        }
    }

    @Override
    public Ret addDistrict(District district) {
        try {
            dictrictDao.addDistrict(new DistrictDomain(district));
        } catch (Exception e) {
            return RetTools.getRet("创建地域失败", e);
        }
        return RetTools.getRet("创建地域成功", null);
    }

    @Override
    public Ret updDistrict(District district) {
        try {
            dictrictDao.updDistrict(new DistrictDomain(district));
        } catch (Exception e) {
            return RetTools.getRet("修改地域失败", e);
        }
        return RetTools.getRet("修改地域成功", null);
    }

    @Override
    public Ret delDistrict(String districtid) {
        try {
            dictrictDao.delDistrict(districtid);
        } catch (Exception e) {
            return RetTools.getRet("删除地域失败", e);
        }
        return RetTools.getRet("删除地域成功", null);

    }

    @Override
    public List selAllDistrict(String districtid) {
        List<DistrictDomain> districtDomains = new ArrayList<DistrictDomain>();
        List  districtList = new ArrayList();
        if("all".equalsIgnoreCase(districtid)){ // all为接口约定的固定值
            districtDomains = dictrictDao.selAllDistrict(districtid);
            if(districtDomains!=null){
                for (int i = 0; i <districtDomains.size(); i++) {
                    District district = new District(districtDomains.get(i));
                    districtList.add(district);
                }
            }
        }
        return districtList;
    }
}
